<script setup lang="ts">
import { Page, useVbenModal } from '@vben/common-ui'

import { message } from 'ant-design-vue'

import { useVbenForm } from '#/adapter/form'
import { editProdInfo } from '#/api/spuBom'

const emit = defineEmits(['refresh'])
const [BaseForm, baseFormApi] = useVbenForm({
  showDefaultActions: false,
  commonConfig: {
    // 在label后显示一个冒号
    colon: true,
    // 所有表单项
  },

  layout: 'horizontal',
  schema: [
    {
      component: 'InputNumber',
      fieldName: 'calculate_order_quantity',
      componentProps: {
        class: 'w-full',
      },
      label: '算料件数',
    },
    {
      component: 'InputNumber',
      fieldName: 'marker_single_piece',
      componentProps: {
        class: 'w-full',
      },
      label: '唛架用量',
    },
    {
      component: 'InputNumber',
      fieldName: 'actual_single_piece',
      componentProps: {
        class: 'w-full',
      },
      label: '实际用量',
    },
    {
      component: 'Switch',
      fieldName: 'not_caigou',
      label: '无需采购',
    },
    {
      component: 'Switch',
      fieldName: 'not_fa',
      label: '无需发料',
    },
  ],
})
const [Modal, modalApi] = useVbenModal({
  onCancel() {
    modalApi.close()
  },
  async onConfirm() {
    const values = await baseFormApi.getValues()
    modalApi.lock(true)
    editProdInfo(values)
      .then((res) => {
        modalApi.unlock()
        if (res.data.code === 1) {
          message.success(res.data.msg)
          emit('refresh')
          modalApi.close()
        } else {
          message.error(res.data.msg)
        }
      })
      .finally(() => {
        modalApi.unlock()
      })
  },
  onOpenChange: async (isOpen: boolean) => {
    if (!isOpen) return
    const { data } = modalApi.getData<Record<string, any>>()
    await baseFormApi.setValues(data, false)
  },
})
</script>

<template>
  <Modal title="修改生产信息">
    <Page content-class="flex flex-col gap-4" description="" title="">
      <BaseForm />
    </Page>
  </Modal>
</template>

<style scoped></style>
